package com.example.tellwin.home;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.apkfuns.logutils.LogUtils;
import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.base.CpBaseFragment;
import com.example.tellwin.base.DaggerRxComponent;
import com.example.tellwin.common.Common;
import com.example.tellwin.home.adpater.QuestionAdapter;
import com.example.tellwin.home.bean.AnswerQuestionBean;
import com.example.tellwin.home.bean.AnswerStatisticsBean;
import com.example.tellwin.home.bean.QuestionBean;
import com.example.tellwin.home.contract.AnswerHomeContract;
import com.example.tellwin.home.presenter.AnwerHomePresenter;
import com.example.tellwin.mine.act.OrderDetailActivity;
import com.example.tellwin.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author : LinZePeng
 * @date : 2020/11/18
 * @Describe ：
 **/
public class TeacherHomeFragment extends CpBaseFragment implements AnswerHomeContract.View {

    @BindView(R.id.fee_time_tv)
    TextView feeTimeTv;
    @BindView(R.id.fee_time_rl)
    RelativeLayout feeTimeRl;
    @BindView(R.id.order_count_rl)
    RelativeLayout orderCountRl;
    @BindView(R.id.flow_statistics_tv)
    TextView flowStatisticsTv;
    @BindView(R.id.flow_statistics_rl)
    RelativeLayout flowStatisticsRl;
    @BindView(R.id.evaluation_satisfaction_number_rl)
    RelativeLayout evaluationSatisfactionNumberRl;
    @BindView(R.id.question_rv)
    RecyclerView questionRv;
    @BindView(R.id.order_count_tv)
    TextView orderCountTv;
    @BindView(R.id.satisfaction_number_tv)
    TextView satisfactionNumberTv;
    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.scrollvew)
    NestedScrollView scrollView;

    private Unbinder unbinder;

    private QuestionAdapter questionAdapter;
    private List<AnswerQuestionBean> questionBeans = new ArrayList<>();

    @Inject
    AnwerHomePresenter mPresenter;

    @Override
    public View onCreateRootView(LayoutInflater inflater, ViewGroup container) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_teacher_home, null);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initEvents();
        initData();
        return view;
    }

    public void refresh(){
        if(mPresenter!=null) {
            mPresenter.getTopStatistics();
            mPresenter.getAnswerQuestionList();
        }
    }

    private void initView() {

        DaggerRxComponent.builder()
                .baseAppComponent(MainApplication.getAppComponent())
                .build()
                .inject(this);
        mPresenter.attachView(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        questionRv.setLayoutManager(linearLayoutManager);
        questionAdapter = new QuestionAdapter();
        questionRv.setAdapter(questionAdapter);
    }

    private void initEvents() {
        questionAdapter.setOnQuestionItemClickListener((position, answerQuestionBean) -> {
            Intent intent = new Intent(getActivity() , OrderDetailActivity.class);
            intent.putExtra(Common.ORDER ,answerQuestionBean.getOrderId());
            startActivity(intent);
        });
        refreshLayout.setOnRefreshListener(() -> {
           mPresenter.getTopStatistics();
           mPresenter.getAnswerQuestionList();
        });

        scrollView.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
          /*  if (scrollY > oldScrollY) {
                LogUtils.i( "Scroll DOWN");
            }
            if (scrollY < oldScrollY) {
                LogUtils.i(  "Scroll UP");
            }

            if (scrollY == 0) {
                LogUtils.i( "TOP SCROLL");
            }*/
            if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
                LogUtils.i( "BOTTOM  SCROLL");
                mPresenter.loadMore();
            }
        });

    }

    RecyclerView.OnScrollListener onScrollListener =new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            if(CommonUtils.isSlideToBottom(questionRv)) {

            }
        }
    };

    private void initData() {
        mPresenter.getTopStatistics();
        mPresenter.getAnswerQuestionList();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void topStatisticsResult(AnswerStatisticsBean answerStatisticsBean) {
        feeTimeTv.setText(answerStatisticsBean.getAnswerDuration() + "");
        orderCountTv.setText(answerStatisticsBean.getOrderCount() + "");
        flowStatisticsTv.setText(answerStatisticsBean.getAnswerIncome() + "");
        satisfactionNumberTv.setText(answerStatisticsBean.getSatisfiedCount() + "");
    }

    @Override
    public void answerQuestionListResult(List<AnswerQuestionBean> answerQuestionBeans) {
        this.questionBeans = answerQuestionBeans;
        questionAdapter.setQuestionBeans(questionBeans);
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void loadMoreResult(List<AnswerQuestionBean> answerQuestionBeans) {
        this.questionBeans.addAll(answerQuestionBeans);
        questionAdapter.notifyDataSetChanged();
    }
}
